package servlet;

import dao.EmpDao;
import entity.Dept;
import entity.Emp;
import org.apache.ibatis.session.SqlSession;
import util.SqlSessionFactoryUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/empadd")
public class EmpAddServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=utf-8");
        String ename = request.getParameter("ename");
        String job = request.getParameter("job");
        String hiredate = request.getParameter("hiredate");
        int sal = Integer.parseInt(request.getParameter("sal"));
        String dname = request.getParameter("dname");
        SqlSession sqlSession= SqlSessionFactoryUtil.getSqlSession();
        Emp emp = new Emp();
        emp.setEname(ename);
        emp.setJob(job);
        emp.setHiredate(hiredate);
        emp.setSal(sal);
        Dept dept = new Dept();
        dept.setDname(dname);
        emp.setDept(dept);
        EmpDao empDao = sqlSession.getMapper(EmpDao.class);
        int num = empDao.addEmp(emp);
        PrintWriter out = response.getWriter();
        out.print(num);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
